package cn.quevo.beetlsql.core;

import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.annotatoin.Table;
import org.beetl.sql.core.kit.StringKit;

import cn.quevo.core.lang.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class PreUnderlinedNameConversion extends UnderlinedNameConversion{
    
    private String tablePre;
    
    public PreUnderlinedNameConversion(String tablePre) {
        this.tablePre = tablePre;
    }
    
    @Override
    public String getTableName(Class<?> c) {
        Table table = (Table)c.getAnnotation(Table.class);
        if(table!=null){
            return table.name();
        }
        String tableName = StringKit.enCodeUnderlined(c.getSimpleName()).toUpperCase();
        return StringUtils.isBlank(tablePre)?tableName:this.tablePre.toUpperCase().concat(tableName);
    }
    
    @Override
    public  String getClassName(String tableName){
         String temp = StringKit.deCodeUnderlined(tableName.toLowerCase());
         temp = StringKit.toUpperCaseFirstOne(temp);
         if(StringUtils.isBlank(tablePre) || !temp.toUpperCase().startsWith(this.tablePre)) {
             return temp;
         }
         return temp.substring(0, tablePre.length());
    }
}
